package com.vdg.fps;

import com.vdg.R;
import com.vdg.fps.config.FPSConfig;
import com.vdg.fps.data.SettingData;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class SettingActivity extends Activity {
	private EditText mEdtPostingThread;
	private EditText mEdtFrom;
	private EditText mEdtTo;
	private CheckBox mChbAutoThumbnail;
	private CheckBox mChbRemoveLink;
	private CheckBox mChbInclubName;
	private EditText mEdtUserAgent;
	private Button mBtnSave;
	private Button mBtnReset;
	private SettingData mSettingData;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_layout);
		init();
	}

	private void init() {
		mEdtPostingThread = (EditText) findViewById(R.id.edt_threads_number);
		mEdtFrom = (EditText) findViewById(R.id.edt_from);
		mEdtTo = (EditText) findViewById(R.id.edt_to);
		mChbAutoThumbnail = (CheckBox) findViewById(R.id.chb_auto_thumnail);
		mChbRemoveLink = (CheckBox) findViewById(R.id.chb_remove_link);
		mChbInclubName = (CheckBox) findViewById(R.id.chb_inclub_name);
		mEdtUserAgent = (EditText) findViewById(R.id.edt_user_agent);
		mBtnReset = (Button) findViewById(R.id.btn_reset);
		mBtnReset.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				loadSetting();
			}
		});
		mBtnSave = (Button) findViewById(R.id.btn_save);
		mBtnSave.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				saveSetting();
			}
		});
		loadSetting();
		setUIFont();
	}

	public void loadSetting() {
		mSettingData = SettingData.getInstance(this);
		if (mSettingData != null) {
			mChbAutoThumbnail.setChecked(mSettingData
					.isAutoAddLinkThumbnailInYourMessage());
			mEdtPostingThread.setText(String.valueOf(mSettingData
					.getPostingThreads()));
			mEdtFrom.setText(String.valueOf(mSettingData.getPostingDelayFrom() / 1000));
			mEdtTo.setText(String.valueOf(mSettingData.getPostingDelayTo() / 1000));
			mChbRemoveLink.setChecked(mSettingData.isRemoveLink());
			mChbInclubName.setChecked(mSettingData
					.isInclubNameWhenExportFacebook());
			mEdtUserAgent.setText(mSettingData.getUserAgent());
		}
	}

	public boolean checkInvalid() {
		if (Long.valueOf(mEdtFrom.getText().toString()) > Long.valueOf(mEdtTo
				.getText().toString())
				|| mEdtFrom.getText().toString().equals("")
				|| mEdtTo.getText().toString().equals("")) {
			Toast.makeText(this,
					getResources().getString(R.string.posting_delay_invalid),
					Toast.LENGTH_LONG).show();
			return true;
		}
		if (mEdtPostingThread.getText().toString().equals("")) {
			Toast.makeText(this,
					getResources().getString(R.string.posting_threads_invalid),
					Toast.LENGTH_LONG).show();
			return true;
		}
		return false;
	}

	public void saveSetting() {
		if (mSettingData != null) {
			if (checkInvalid()) {
				return;
			}
			if (!mEdtPostingThread.getText().toString().equals("")) {
				mSettingData
						.setPostingThreads(Integer.parseInt(mEdtPostingThread
								.getText().toString()), this);
			}
			if (!mEdtFrom.getText().toString().equals("")) {
				mSettingData.setPostingDelayFrom(
						Long.valueOf(mEdtFrom.getText().toString()) * 1000,
						this);
			}
			if (!mEdtTo.getText().toString().equals("")) {
				mSettingData.setPostingDelayTo(
						Long.valueOf(mEdtTo.getText().toString()) * 1000, this);
			}
			mSettingData.setAutoAddLinkThumbnailInYourMessage(
					mChbAutoThumbnail.isChecked(), this);
			mSettingData.setRemoveLink(mChbRemoveLink.isChecked());
			mSettingData.setInclubNameWhenExportFacebook(
					mChbInclubName.isChecked(), this);
			if (!mEdtUserAgent.getText().toString().equals("")) {
				mSettingData.setUserAgent(mEdtUserAgent.getText().toString(),
						this);
			}
			Toast.makeText(this,
					getResources().getString(R.string.save_success),
					Toast.LENGTH_LONG).show();
			;
		} else {
			Toast.makeText(this,
					getResources().getString(R.string.save_failed),
					Toast.LENGTH_LONG).show();
		}
	}

	private void setUIFont() {
		Typeface type = Typeface
				.createFromAsset(getAssets(), FPSConfig.UI_FONT);
		mEdtPostingThread.setTypeface(type);
		mEdtFrom.setTypeface(type);
		mEdtTo.setTypeface(type);
		mChbAutoThumbnail.setTypeface(type);
		mChbRemoveLink.setTypeface(type);
		mChbInclubName.setTypeface(type);
		mEdtUserAgent.setTypeface(type);
		mBtnSave.setTypeface(type);
		mBtnReset.setTypeface(type);
	}

}
